﻿using CastingWellEquipmentGUI.Config;
using CastingWellEquipmentGUI.Model;
using CastingWellEquipmentGUI.Utils;
using CastingWellEquipmentGUI.ViewModel.Common.Command;
using CastingWellEquipmentGUI.ViewModel.Common.WindowsManager;
using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;

namespace CastingWellEquipmentGUI.ViewModel.Pages
{
    /// <summary>
    /// 预检测ViewModel
    /// </summary>
    public class PreCheckPageViewModel : ViewModelBase
    {
        #region 变量/常量
        /// <summary>
        /// 当前窗体名称
        /// </summary>
        private const string showPageTitle = "PreCheckPage";
        /// <summary>
        /// 检验清单 true=确认，false=未确认
        /// </summary>
        private bool _boolCheckOrder = false;
        /// <summary>
        /// 检验通过 true=通过，false=不通过
        /// </summary>
        private bool _boolAllCheckPass;
        #endregion 变量/常量

        #region 页面属性
        private string _Lable_CastingNo = "";
        /// <summary>
        /// 铸造编号
        /// </summary>
        public string Lable_CastingNo
        {
            get { return _Lable_CastingNo; }
            set { _Lable_CastingNo = value; RaisePropertyChanged("Lable_CastingNo"); }
        }

        #region 检测项图片
        private Brush _Color_CastingNo = Brushes.Red;
        /// <summary>
        /// 铸造编号检测
        /// </summary>
        public Brush Color_CastingNo
        {
            get { return _Color_CastingNo; }
            set { _Color_CastingNo = value; RaisePropertyChanged("Color_CastingNo"); }
        }
        private Brush _Color_FormulaVerification = Brushes.Red;
        /// <summary>
        /// 配方校验
        /// </summary>
        public Brush Color_FormulaVerification
        {
            get { return _Color_FormulaVerification; }
            set { _Color_FormulaVerification = value; RaisePropertyChanged("Color_FormulaVerification"); }
        }
        private Brush _Color_HydraulicSystem = Brushes.Red;
        /// <summary>
        /// 液压系统
        /// </summary>
        public Brush Color_HydraulicSystem
        {
            get { return _Color_HydraulicSystem; }
            set { _Color_HydraulicSystem = value; RaisePropertyChanged("Color_HydraulicSystem"); }
        }
        private Brush _Color_WaterSupplySystem = Brushes.Red;
        /// <summary>
        /// 水系统
        /// </summary>
        public Brush Color_WaterSupplySystem
        {
            get { return _Color_WaterSupplySystem; }
            set { _Color_WaterSupplySystem = value; RaisePropertyChanged("Color_WaterSupplySystem"); }
        }
        private Brush _Color_Launder = Brushes.Red;
        /// <summary>
        /// 流槽
        /// </summary>
        public Brush Color_Launder
        {
            get { return _Color_Launder; }
            set { _Color_Launder = value; RaisePropertyChanged("Color_Launder"); }
        }
        private Brush _Color_NetView = Brushes.Red;
        /// <summary>
        /// 网络视图
        /// </summary>
        public Brush Color_NetView
        {
            get { return _Color_NetView; }
            set { _Color_NetView = value; RaisePropertyChanged("Color_NetView"); }
        }
        private Brush _Color_HoldingFurnace = Brushes.Red;
        /// <summary>
        /// 保温炉
        /// </summary>
        public Brush Color_HoldingFurnace
        {
            get { return _Color_HoldingFurnace; }
            set { _Color_HoldingFurnace = value; RaisePropertyChanged("Color_HoldingFurnace"); }
        }
        private Brush _Color_CastWellPublicAuxiliary = Brushes.Red;
        /// <summary>
        /// 铸井公辅
        /// </summary>
        public Brush Color_CastWellPublicAuxiliary
        {
            get { return _Color_CastWellPublicAuxiliary; }
            set { _Color_CastWellPublicAuxiliary = value; RaisePropertyChanged("Color_CastWellPublicAuxiliary"); }
        }
        private Brush _Color_CastWellPublic = Brushes.Red;
        /// <summary>
        /// 铸井公共
        /// </summary>
        public Brush Color_CastWellPublic
        {
            get { return _Color_CastWellPublic; }
            set { _Color_CastWellPublic = value; RaisePropertyChanged("Color_CastWellPublic"); }
        }
        private Brush _Color_CheckOrder = Brushes.Red;
        /// <summary>
        /// 检查清单
        /// </summary>
        public Brush Color_CheckOrder
        {
            get { return _Color_CheckOrder; }
            set { _Color_CheckOrder = value; RaisePropertyChanged("Color_CheckOrder"); }
        }
        #endregion 检测项图片

        #endregion 页面属性

        #region 定义页面执行命令
        /// <summary>
        /// 铸造编号命令
        /// </summary>
        public DelegateCommand ShowEditCastingNoPanelCommand { get; set; }
        /// <summary>
        /// 检查清单命令
        /// </summary>
        public DelegateCommand CheckOrderCommand { get; set; }
        /// <summary>
        /// 检测确认命令
        /// </summary>
        public DelegateCommand CheckConfirmCommand { get; set; }
        /// <summary>
        /// 跳转页面名称
        /// </summary>
        public DelegateCommand GoToPageCommand { get; set; }
        /// <summary>
        /// 隐藏检查清单页
        /// </summary>
        public DelegateCommand HideFeederPageSettingPanel1WindowCommand { get; set; }
        /// <summary>
        /// 检查清单确认
        /// </summary>
        public DelegateCommand CheckOkCommand { get; set; }
        #endregion 定义页面执行命令

        #region 方法
        /// <summary>
        /// 构造函数
        /// </summary>
        public PreCheckPageViewModel()
        {
            // 初始化命令
            initCommand();
            // 预检测
            preCheck();
            // 消息通道注册
            initMessenger();
            
        }

        /// <summary>
        /// 消息通道注册
        /// ViewModel之间数据通信使用，整个项目全局唯一
        /// </summary>
        private void initMessenger()
        {
            // 注册获取新增铸造编号
            Messenger.Default.Register<string>(this, "PreCheckPageCastingNo", (message) =>
            {
                preCheck();
            });
        }

        /// <summary>
        /// 初始化命令
        /// </summary>
        private void initCommand()
        {
            // 页面跳转
            GoToPageCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(changeTabHandler)
            };
            // 铸造标号命令
            ShowEditCastingNoPanelCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(ShowEditCastingNoPanelHandler)
            };
            // 检查清单命令
            CheckOrderCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(CheckOrderHandler)
            };
            // 检测确认命令
            CheckConfirmCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(CheckConfirmHandler)
            };
            // 隐藏检查清单页
            HideFeederPageSettingPanel1WindowCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(hidePageSettingPanel1Handler)
            };
            // 检查清单确认
            CheckOkCommand = new DelegateCommand()
            {
                ExecuteActionObj = new Action<object>(checkOkHandler)
            };
        }

        /// <summary>
        /// 检查清单确认
        /// </summary>
        /// <param name="obj"></param>
        private void checkOkHandler(object obj)
        {
            _boolCheckOrder = true;
            Color_CheckOrder = Brushes.Green;
            Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, "hideCheckOrderWindowMessage"));
        }
        /// <summary>
        /// 隐藏命令处理器
        /// </summary>
        /// <param name="obj"></param>
        private void hidePageSettingPanel1Handler(object obj)
        {
            _boolCheckOrder = false;
            Color_CheckOrder = Brushes.Red;
            Messenger.Default.Send<NotificationMessage>(new NotificationMessage(this, "hideCheckOrderWindowMessage"));
        }

        /// <summary>
        /// 铸造编号命令
        /// </summary>
        /// <param name="obj"></param>
        private void ShowEditCastingNoPanelHandler(object obj)
        {
            WindowManager.ShowDialog("EditCastingNoPanelWindow", null);
        }

        /// <summary>
        /// 检查清单命令
        /// </summary>
        /// <param name="obj"></param>
        private void CheckOrderHandler(object obj)
        {
            WindowManager.ShowDialog("CheckOrderWindow", null);
        }

        /// <summary>
        /// 检测确认命令
        /// </summary>
        /// <param name="obj"></param>
        private void CheckConfirmHandler(object obj)
        {
            // 预检测
            preCheck();
            if (_boolAllCheckPass)
            {
                // 确认通过
                if (KafkaHelper.SendGatewayChangeTagMessage(showPageTitle, "Button_CheckConfirmation", "true"))
                {
                    MessageBoxResult result = MessageBox.Show("检测确认成功！", "信息提示", MessageBoxButton.OK, MessageBoxImage.Information);
                }
                else
                {
                    // 确认通过失败
                    MessageBoxResult result = MessageBox.Show("检测确认失败！", "信息提示", MessageBoxButton.OK, MessageBoxImage.Error);
                }
            }
            else
            {
                // 提示：存在检测未通过项目
                MessageBoxResult result = MessageBox.Show("存在预检测失败项，无法执行检测确认！", "信息提示", MessageBoxButton.OK, MessageBoxImage.Warning);
            }
        }

        /// <summary>
        /// 预检测
        /// </summary>
        private void preCheck()
        {
            // 扫描页面检测项目
            // 检测校验铸造编号
            string strCastingNo = SystemConfig.GetCastingNo();
            if (strCastingNo != null && strCastingNo != string.Empty)
            {
                Lable_CastingNo = strCastingNo;
                Color_CastingNo = Brushes.Green;
            }
            else
            {
                Lable_CastingNo = "";
                Color_CastingNo = Brushes.Red;
                _boolAllCheckPass = false;
            }
            // 配方校验
            if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Button_FormulaVerification") == "true")
            {
                Color_FormulaVerification = Brushes.Green;
            }
            else
            {
                Color_FormulaVerification = Brushes.Red;
                _boolAllCheckPass = false;
            }
            // 液压系统
            if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Button_HydraulicSystem") == "true")
            {
                Color_HydraulicSystem = Brushes.Green;
            }
            else
            {
                Color_HydraulicSystem = Brushes.Red;
                _boolAllCheckPass = false;
            }
            // 水系统
            if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Button_WaterSupplySystem") == "true")
            {
                Color_WaterSupplySystem = Brushes.Green;
            }
            else
            {
                Color_WaterSupplySystem = Brushes.Red;
                _boolAllCheckPass = false;
            }
            // 流槽
            if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Button_Launder") == "true")
            {
                Color_Launder = Brushes.Green;
            }
            else
            {
                Color_Launder = Brushes.Red;
                _boolAllCheckPass = false;
            }
            // 网络视图
            if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Button_NetView") == "true")
            {
                Color_NetView = Brushes.Green;
            }
            else
            {
                Color_NetView = Brushes.Red;
                _boolAllCheckPass = false;
            }
            // 保温炉
            if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Button_HoldingFurnace") == "true")
            {
                Color_HoldingFurnace = Brushes.Green;
            }
            else
            {
                Color_HoldingFurnace = Brushes.Red;
                _boolAllCheckPass = false;
            }
            // 铸井公辅
            if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Button_CastWellPublicAuxiliary") == "true")
            {
                Color_CastWellPublicAuxiliary = Brushes.Green;
            }
            else
            {
                Color_CastWellPublicAuxiliary = Brushes.Red;
                _boolAllCheckPass = false;
            }
            // 铸井公共
            if (TagTool.GetTagValueByFormNameAndControlName(showPageTitle, "Button_CastWellPublic") == "true")
            {
                Color_CastWellPublic = Brushes.Green;
            }
            else
            {
                Color_CastWellPublic = Brushes.Red;
                _boolAllCheckPass = false;
            }
            // 检查清单
            if (_boolCheckOrder == true)
            {
                Color_CheckOrder = Brushes.Green;
            }
            else
            {
                Color_CheckOrder = Brushes.Red;
                _boolAllCheckPass = false;
            }
        }

        /// <summary>
        /// 页面切换
        /// </summary>
        /// <param name="obj"></param>
        private void changeTabHandler(object obj)
        {
            Messenger.Default.Send<string>(obj.ToString(), "GoToPage");
        }
        #endregion 方法

    }
}
